package com.ysm.smfu.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.ysm.smfu.R;
import com.ysm.smfu.activity.My_XiuGai_Activity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.TimeButton;
import com.ysm.smfu.welcome.SharedConfig;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/9/28.
 */
public class PhoneNumSetFragment extends MyBaseFragment {


    @BindView(R.id.et_activity_register_phonenumber)
    EditText etActivityRegisterPhonenumber;
    @BindView(R.id.et_activity_register_yanzhengma)
    EditText etActivityRegisterYanzhengma;
    @BindView(R.id.btn_activity_register_getyanzhengma)
    TimeButton btnActivityRegisterGetyanzhengma;
    private Bundle savedInstanceState;

    /**
     * 电话正则
     */
    String numRegex = "(0\\d{2,3}-\\d{7,8})|(1[3584]\\d{9})";
    private String types = "phone";
    private String code;
    private String phoneNum;
    private String realnum;
    private String userId;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.fragment_psd_login);
        ButterKnife.bind(this, view);
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        String name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        userId = currShared.getString(SpKey.USERID, "");
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        UIUtils.showKeyboard(etActivityRegisterPhonenumber);
    }

    @OnClick({R.id.btn_activity_register_getyanzhengma, R.id.fragment_zhifu_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_activity_register_getyanzhengma:
                phoneNum = etActivityRegisterPhonenumber.getText().toString().trim();
                realnum = phoneNum.replace(" ", "");
                if (!realnum.matches(SpKey.NUMREGEX)) {
                    break;
                }
                if (!btnActivityRegisterGetyanzhengma.isTime()) {
                    btnActivityRegisterGetyanzhengma.startTime();
                }
                // 获取验证码
                getYzCode();
                break;
            case R.id.fragment_zhifu_next:
                String code = etActivityRegisterYanzhengma.getText().toString().trim();
                chechYzCode(code);
        }
    }

    /**
     * 获取验证码
     */
    private void getYzCode() {
        BaseProtocal.builder(false).getYzCode(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
//                            判断是否手机已注册的code
                String phoneCode = JsonUtil.getCode(response);
                if (TextUtils.equals(phoneCode, "100001")) {
                    code = JsonUtil.getYzCode(response);
                    etActivityRegisterYanzhengma.requestFocus();
                } else {
                    // 如果用户以注册，则停止读秒
                    btnActivityRegisterGetyanzhengma.endTime();
                    return;
                }
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showErrToast();
            }
        }, realnum, types, null, null);
    }

    /**
     * 检测验证码
     *
     * @param code
     * @return
     */
    public void chechYzCode(String code) {
        BaseProtocal.builder(false).checkYzCode(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {

                String msg = JsonUtil.getMessage(response);
                if (TextUtils.equals(msg, "验证码失效!")) {
                    ToastUtils.showToast("验证码失效，请重新获取");
                    return;
                }
                UIUtils.startActivity(My_XiuGai_Activity.class, "position", 9);
            }

            @Override
            public void onFail(Exception e) {

            }
        }, realnum,code,types,null,null);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        打个补丁
        if (btnActivityRegisterGetyanzhengma != null)
            btnActivityRegisterGetyanzhengma.onDestroy();
    }
}
